package com.woniuxy.woniuhis.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.MediItem;
import com.woniuxy.woniuhis.entity.RecipeMedi;
import com.woniuxy.woniuhis.exception.RecipeMediServiceException;
import com.woniuxy.woniuhis.mapper.MediItemMapper;
import com.woniuxy.woniuhis.mapper.RecipeMediMapper;
import com.woniuxy.woniuhis.service.RecipeMediService;
import com.woniuxy.woniuhis.util.UUIDUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RecipeMediServiceImpl implements RecipeMediService {
    @Autowired
    private RecipeMediMapper recipeMediMapper;
    @Autowired
    private MediItemMapper mediItemMapper;

    @Override
    public void addRecipeMedi(RecipeMedi recipeMedi) {
        try {
            if (recipeMedi.getRecipeMediId() == null) {
                recipeMedi.setRecipeMediId(UUIDUtil.uuid32());
            }
            recipeMediMapper.insertRecipeMedi(recipeMedi);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RecipeMediServiceException("服务维护中");
        }
    }

    @Override
    public void delRecipeMediById(String recipeMediId) {
        try {
            if (!StringUtils.isEmpty(recipeMediId)) {
                recipeMediMapper.deleteRecipeMediById(recipeMediId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RecipeMediServiceException("服务维护中");
        }
    }

    @Override
    public void modifyRecipeMedi(RecipeMedi recipeMedi) {
        try {
            if (recipeMedi != null) {
                recipeMediMapper.updateRecipeMedi(recipeMedi);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RecipeMediServiceException("服务维护中");
        }
    }

    @Override
    public RecipeMedi findRecipeMediById(String recipeMediId) {
        try {
            if (!StringUtils.isEmpty(recipeMediId)) {
                RecipeMedi recipeMedi = recipeMediMapper.selectRecipeMediById(recipeMediId);
                List<MediItem> mediItemList = mediItemMapper.selectMediItemListByRecipeMediId(recipeMediId);
                recipeMedi.setMediItemList(mediItemList);
                return recipeMedi;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RecipeMediServiceException("服务维护中");
        }
    }

    @Override
    public PageInfo<RecipeMedi> findRecipeMediListByCondition(RecipeMedi condition, Integer pageNum, Integer pageSize) {
        try {
            if (pageNum == null) {
                pageNum = 0;
            }
            if (pageSize == null || pageSize <= 0) {
                //默认返回五条数据
                pageSize = 5;
            }
            return new PageInfo<RecipeMedi>(recipeMediMapper.selectRecipeMediListByCondition(condition, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RecipeMediServiceException("服务维护中");
        }
    }
}
